package com.gitee.chili.leisure.api.account;

import cn.hutool.json.JSONObject;
import com.gitee.chili.leisure.ResponseResult;
import com.gitee.chili.leisure.annotation.PathRestController;
import com.gitee.chili.leisure.domain.Account;
import com.gitee.chili.leisure.enums.CurrencyToken;
import com.gitee.chili.leisure.service.IAccountService;
import com.gitee.chili.leisure.util.Enum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * AccountController
 *
 * @author week solider
 * @since 2020.09.01
 */
@Validated
@PathRestController("/account")
public class AccountController {

    @Autowired
    private IAccountService accountService;

    @PostMapping("/getAccountAll")
    public ResponseResult<List<Account>> getAccountAll(@Valid JSONObject param) {
        long memberId = Long.parseLong(param.get("userId").toString());
        if (param.get("tokens") instanceof List) {

//            @SuppressWarnings("unchecked")
            List<?> tokens = (List<?>)param.get("tokens");

            Account account = new Account();


//            accountService.getRepository().findAll(Example.of());

        } else {

        }
        return ResponseResult.success();
    }

    @PostMapping("/getAccount")
    public ResponseResult<Account> getAccount(@Valid JSONObject param) {
        Long memberId = Long.parseLong(param.get("userId").toString());
        Integer token = Integer.parseInt(param.get("token").toString());

        CurrencyToken currencyToken = Enum.valueOf(CurrencyToken.class, t -> t.getCode().equals(token));

        Optional<Account> optionalAccount = accountService.getBalance(currencyToken, memberId);

        if(optionalAccount.isPresent()){
            return ResponseResult.success(optionalAccount.get());
        }

        return ResponseResult.failure("账户信息未找到, 请核实账户信息!");
    }
}
